<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 二维码
 */
namespace app\user\model;

use Mscore\Core\Lib\Qrcode;

class Qrocde
{
    /**
     * 输出log二维码
     * @param $img
    */
    public function qrcodelog($data){
        $value = $data['data']; //二维码内容
        $logoback = isset($data['logurl'])&&!empty($data['logurl'])?$data['logurl']:"/static/qrcode/logoback.png";
        $logo = isset($data['logo'])&&!empty($data['logo'])?$data['logo']:"/static/qrcode/logo.png";
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 50;//生成图片大小
        //生成二维码图片
        Qrcode::png($value, config('file.path').'/static/qrcode/qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
  
        $logo = config('file.path').$logo;//准备好的logo图片
        $logo = imagecreatefromstring(file_get_contents($logo));
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度

        $image = imagecreatetruecolor($logo_width+10, $logo_height+10);//图片大小
        $white = imagecolorallocate($image, 213, 213, 213);//图片的rgb
        imagefill($image, 0, 0, $white);

        imagecopyresampled($image, $logo, 5, 5, 0, 0, $logo_width,$logo_height, $logo_width, $logo_height);
        //保存图片
        imagepng($image, config('file.path').$logoback); 

        $logo = config('file.path').$logoback;//准备好的logo图片
        $QR = config('file.path').'/static/qrcode/qrcode.png';//已经生成的原始二维码图
        if ($logo !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 10;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        }
            //输出图片
            imagepng($QR, config('file.path').'/static/qrcode/'.$data['name'].'.png');

            return '/static/qrcode/'.$data['name'].'.png';
    }

    /**
     * 输出二维码
     * @param $img
    */
    public function qrcode($data){
        $value = $data['data']; //二维码内容
        $data['logurl'] = $data['logurl'];
        $data['qrurl'] =  isset($data['qrurl'])&&!empty($data['qrurl'])?$data['qrurl']:"/static/qrcode/".$data['aid'].".png";
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 50;//生成图片大小
        //生成二维码图片
        Qrcode::png($value, config('file.path').$data['qrurl'], $errorCorrectionLevel, $matrixPointSize, 2);
        $QR = config('file.path').$data['qrurl'];
        $data['qrcode']=$data['qrurl'];
        $logo=config('file.path') . $data['logurl'];
        if (!empty($data['qrcode']) && file_exists($logo) !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = @imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 8;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            @imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
            //输出图片
            imagepng($QR, config('file.path').$data['qrcode']);
        }
        return '/'.$data['qrcode'];
            
    }

    /**
     * 合成分享卡
     * @param $img
    */
    public function sharepic($data){ 

        $qrcode = config('file.path').$data['qrcode'];//准备好的二维码图片
        $back = config('file.path').$data['back'];//已有的背景图片
        if ($qrcode !== FALSE) {
            $back = imagecreatefromstring(file_get_contents($back));
            $qrcode = imagecreatefromstring(file_get_contents($qrcode));
            $back_width = imagesx($back);//背景图片宽度
            $back_height = imagesy($back);//背景图片高度
            $qrcode_width = imagesx($qrcode);//二维码图片宽度
            $qrcode_height = imagesy($qrcode);//二维码图片高度
            $qrcode_back_width = $back_width / 3;
            $scale = $qrcode_width/$qrcode_back_width;
            $qrcode_back_height = $qrcode_height/$scale;
            $from_width = ($back_width - $qrcode_back_width) / 2;
            $from_height = ($back_height - $qrcode_back_height) / 1.5;
            //重新组合图片并调整大小
            @imagecopyresampled($back, $qrcode, $from_width, $from_height, 0, 0, $qrcode_back_width,$qrcode_back_height, $qrcode_width, $qrcode_height);
        }
            //输出图片
            imagepng($back, config('file.path').$data['share']);
            return '/'.$data['share'];
    }


}
